<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://demo.thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\store\controller;

use library\Controller;
use library\tools\Data;
use service\ToolsService;
use think\Db;

/**
 * 碎片信息管理
 * Class Fragment
 * @package app\stupplier\controller
 */
class Fragment extends Controller
{
    /**
     * 指定数据表
     * @var string
     */
    protected $table = 'te_fragment';

    /**
     * 碎片管理
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        $this->title = '碎片管理';
        $query = $this->_query($this->table)->like('name')->equal("type");
        $query->order('id desc')->where(['is_deleted'=>0])->page();
    }

    /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _index_page_filter(&$data)
    {
        $this->type=[''=>'全部', 1 => '单行文本', 2 => '多行文本', 3 => '图片', '4' => '富文本'];
    }
    /**
     * 添加碎片信息
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function add()
    {
        $this->title = '添加碎片信息';
        $this->isAddMode = '1';
        $this->_form($this->table, 'form');
    }

    /**
     * 编辑碎片信息
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit()
    {
        $this->title = '编辑碎片信息';
        $this->isAddMode = '0';
        $this->_form($this->table, 'form');
    }

    /**
     * 表单数据处理
     * @param array $data
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    protected function _form_filter(&$data)
    {
        $this->type=[0=>'选择类型', 1 => '单行文本', 2 => '多行文本', 3 => '图片', '4' => '富文本'];
        if ($this->request->isPost()) {
//            if (isset($data['id']) && ($data['id'] == 10 || $data['id'] == 5)){
//
//              if ($data['content'] >= 100){
//                  $this->error('比例不能超过100');
//              }
//
//            }
            empty($data['type']) && $this->error('请选择碎片类型');
            empty($data['content']) && $this->error('碎片内容不能为空');
        }
    }

    /**
     * 表单结果处理
     * @param boolean $result
     */
    protected function _form_result($result)
    {
        if ($result && $this->request->isPost()) {
            $this->success('碎片编辑成功！', 'javascript:history.back()');
        }
    }

}
